package com.jp.vn.main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Timer;
import java.util.TimerTask;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.vn.Object.Data;

public class NumberPractiseActivity extends BaseActivity implements OnClickListener {

	TextView tfNum, tfClock, tfScore;
	EditText tfAns;
	Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnOk,
			btnDel;
	ImageView btAudio;
	ArrayList<String> arrNum;
	MediaPlayer mediaPlayer = null;
	static String ques;
	int idQues = 0;
	int score = 0;
	int clock = 0;
	Timer time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_number_practise);

		tfNum = (TextView) findViewById(R.id.tfNum);
		tfClock = (TextView) findViewById(R.id.tfClock);
		tfScore = (TextView) findViewById(R.id.tfScore);
		tfAns = (EditText) findViewById(R.id.tfAns);
		btn0 = (Button) findViewById(R.id.btn0);
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn3 = (Button) findViewById(R.id.btn3);
		btn4 = (Button) findViewById(R.id.btn4);
		btn5 = (Button) findViewById(R.id.btn5);
		btn6 = (Button) findViewById(R.id.btn6);
		btn7 = (Button) findViewById(R.id.btn7);
		btn8 = (Button) findViewById(R.id.btn8);
		btn9 = (Button) findViewById(R.id.btn9);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnDel = (Button) findViewById(R.id.btnback);
		btAudio = (ImageView) findViewById(R.id.btAudio);

		tfAns.setInputType(InputType.TYPE_NULL);
		btn0.setOnClickListener(this);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
		btn7.setOnClickListener(this);
		btn8.setOnClickListener(this);
		btn9.setOnClickListener(this);
		btnOk.setOnClickListener(this);
		btnDel.setOnClickListener(this);
		btAudio.setOnClickListener(this);

		tfScore.setText(score + "");
		tfClock.setText(clock + "");
		tfNum.setText("Câu " + (idQues + 1) + "/15");
		arrNum = new ArrayList<String>(Arrays.asList(Data.arrayNum));
		Collections.shuffle(arrNum);
		getQuestion(idQues);
		time = new Timer();
		time.scheduleAtFixedRate(new TimerTask() {         
		        @Override
		        public void run() {
		            runOnUiThread(new Runnable()
		            {
		                @Override
		                public void run()
		                {
		                   ++clock;
		                   tfClock.setText(clock+"");
		                }
		            });
		        }
		    }, 1000, 1000);
		
	}

	public void getQuestion(int id) {
		ques = arrNum.get(id);
		try {
			playSound(ques);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void playSound(String fileName) throws IOException {
		AssetFileDescriptor afd = getAssets().openFd(
				"sodem/" + fileName + ".ogg");
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setDataSource(afd.getFileDescriptor(),
				afd.getStartOffset(), afd.getLength());
		mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mp.start();
			}
		});

		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer mp) {
						mp.release();
						mediaPlayer = null;
					}
				});

		mediaPlayer.prepareAsync();
	}

	public void stopPlaying() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		switch (id) {
		case R.id.btn0:
			tfAns.setText(tfAns.getText().toString() + btn0.getText());
			break;
		case R.id.btn1:
			tfAns.setText(tfAns.getText().toString() + btn1.getText());
			break;
		case R.id.btn2:
			tfAns.setText(tfAns.getText().toString() + btn2.getText());
			break;
		case R.id.btn3:
			tfAns.setText(tfAns.getText().toString() + btn3.getText());
			break;
		case R.id.btn4:
			tfAns.setText(tfAns.getText().toString() + btn4.getText());
			break;
		case R.id.btn5:
			tfAns.setText(tfAns.getText().toString() + btn5.getText());
			break;
		case R.id.btn6:
			tfAns.setText(tfAns.getText().toString() + btn6.getText());
			break;
		case R.id.btn7:
			tfAns.setText(tfAns.getText().toString() + btn7.getText());
			break;
		case R.id.btn8:
			tfAns.setText(tfAns.getText().toString() + btn8.getText());
			break;
		case R.id.btn9:
			tfAns.setText(tfAns.getText().toString() + btn9.getText());
			break;
		case R.id.btnOk:
			if (tfAns.getText().toString().length() > 0) {
				++idQues;
				if (tfAns.getText().toString().equals(ques)) {
					score = score + 1;
					tfScore.setText(score + "");
				}
				if (idQues != 15) {
					tfNum.setText("Câu " + (idQues + 1) + "/15");
					tfAns.setText("");
					getQuestion(idQues);
				} else {
					time.cancel();
					int wrongAns =  15 - score;
					showDialogScore(this,clock,score,wrongAns);
				}
			}
			break;
		case R.id.btnback:
			String ans = tfAns.getText().toString();
			if (ans.length() > 0) {
				String newAns = ans.substring(0, tfAns.length() - 1);
				tfAns.setText(newAns);
			}
			break;
		case R.id.btAudio:
			try {
				playSound(ques);
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
	}
}
